"Write a short bit of code to loop from 0 to 10 and if the number is 3 then write (third)"
"run" counter
...
...
For-loop Ideas
Calculate Total Price:
Concept: Use a for loop to iterate over an array of numbers (prices like $1.50, $4.99, $2.00).
Action: The loop will add each number to a running total variable.
Output: The final sum (the total price) displayed in a paragraph or heading. π°
Display Student Grades (with Pass/Fail):
Concept: Use a for loop and an if/else statement inside the loop to check an array of numbers (grades).
Action: Iterate through the grades. If a grade is 70 or higher, display it as "Pass"; otherwise, display it as "Fail."
Output: A list showing each grade's outcome (e.g., "85: Pass," "62: Fail"). π
Reverse a List of Names:
Concept: Use a for loop that iterates backward over an array of strings (names).
Action: The loop starts at the last index of the array and decrements, adding the names to a new array or directly to the HTML in reverse order.
Output: A list of names displayed from last to first. π
Filter Even Numbers:
Concept: Use a for loop with a modulo operator (%) to check an array of integers.
Action: The loop checks if the number divided by 2 has a remainder of 0. If it does, the number is added to a new array or displayed.
Output: A list containing only the even numbers from the original array.
Create a Simple Multiplication Table:
Concept: Use a for loop to iterate from 1 to 10.
Action: Inside the loop, calculate and display the product of the loop counter and a chosen number (e.g., 7).
Output: A list like: "1 x 7 = 7," "2 x 7 = 14," etc. π’
Count Vowels in a Sentence Array:
Concept: Use a nested loopβan outer for loop for an array of strings (sentences) and an inner loop for the characters in each string.
Action: The inner loop checks if the character is a vowel (a, e, i, o, u) and increments a counter.
Output: For each sentence, display the total count of vowels.
Power Calculation Table:
Concept: Use a for loop to calculate powers of a base number (e.g., 2).
Action: Iterate from 1 to 5. Inside the loop, use Math.pow() or repeated multiplication to calculate:
21, 22, 23.
, etc., and display the results.
Output: A list showing the base number raised to increasing powers.
Highlight Words Shorter Than 5 Letters:
Concept: Use a for loop to iterate over an array of strings (words) and check the length property.
Action: The loop checks if word.length < 5. If true, the word is displayed with an emphasis (e.g., wrapped in tags); otherwise, it's displayed normally. Output: A sentence or list where short words are visually emphasized.
Populate an HTML select Dropdown:
Concept: Use a for loop to iterate over an array of strings (e.g., years 2025 to 2035).
Action: For each year in the array, the loop dynamically creates a new option element and appends it to a pre-existing select (dropdown) element on the page.
Output: A fully functional dropdown menu populated with the array's values. π